package com.open.demo.apm

import android.os.Handler
import android.os.Looper
import android.os.Message
import android.os.MessageQueue
import android.os.SystemClock
import android.os.Trace
import com.open.demo.joor.Reflect
import com.tencent.matrix.trace.core.LooperMonitor

/**
 *
 * @Author: hailong.qiu hailong.qiu@xgimi.com
 * @Maintainer: hailong.qiu hailong.qiu@xgimi.com
 * @Date: 2021/12/2
 * @Copyright: 2021 www.xgimi.com Inc. All rights reserved.
 */
class APM {

    var applicationCreateBeginTime:Long? = null

    fun hackSysHandlerCallback() {
        applicationCreateBeginTime = SystemClock.uptimeMillis()
        Reflect.on("android.app.ActivityThread")
            .field("sCurrentActivityThread")
            .field("mH")
            .field("mCallback")
//        Trace.beginSection()
    }

    class HackCallback : Handler.Callback {
        var originalCallback:Handler.Callback? = null
        constructor(callback:Handler.Callback) {
            originalCallback = callback
        }
        override fun handleMessage(msg: Message): Boolean {
            return null != originalCallback && originalCallback?.handleMessage(msg) == true
        }
    }

    fun test() {
        val looperMoitor = ApmLooperMonitor(Looper.getMainLooper())
        Looper.myQueue().addIdleHandler {
            false
        }
    }
}